package com.ecoolex.ying.space.ws;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.ecoolex.framework.common.exception.BizException;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.ying.space.core.manager.OutsourcingGoodsManager;
import com.ecoolex.ying.space.core.manager.OutsourcingPackagedGoodsManager;
import com.ecoolex.ying.space.core.manager.OutsourcingPackagedManager;
import com.ecoolex.ying.space.core.manager.UserWorksManager;
import com.ecoolex.ying.space.core.manager.WholeHouseCaseManager;
import com.ecoolex.ying.space.entity.OutsourcingGoodsWithBLOBs;
import com.ecoolex.ying.space.entity.OutsourcingPackaged;
import com.ecoolex.ying.space.entity.OutsourcingPackagedGoods;
import com.ecoolex.ying.space.entity.UserWorks;
import com.ecoolex.ying.space.entity.WholeHouseCase;
import com.ecoolex.ying.space.enums.UserWorksType;
import com.ecoolex.ying.space.pojo.QueryOutsourcingGoodsAndPackagedDTO;
import com.ecoolex.ying.space.pojo.QueryWholeHouseCaseAndUserWorksDTO;

@Service
public class OutsourcingGoodsServiceImpl implements OutsourcingGoodsService{
	
	Logger log = LoggerFactory.getLogger(OutsourcingGoodsServiceImpl.class);
	
	@Autowired
	private OutsourcingGoodsManager outsourcingGoodsManager;

	@Override
	public OutsourcingGoodsWithBLOBs addOutsourcingGoods(OutsourcingGoodsWithBLOBs record, String createUser)
			throws BizException {
		return outsourcingGoodsManager.add(record, createUser);
	}

	@Override
	public OutsourcingGoodsWithBLOBs editOutsourcingGoods(OutsourcingGoodsWithBLOBs record, String updateUser)
			throws BizException {
		return outsourcingGoodsManager.edit(record, updateUser);
	}

	@Override
	public void deleteOutsourcingGoods(OutsourcingGoodsWithBLOBs record, String updateUser) throws BizException {
		outsourcingGoodsManager.delete(record, updateUser);
	}

	@Override
	public OutsourcingGoodsWithBLOBs getOutsourcingGoodsById(Integer outsourcingGoodsId) {
		return outsourcingGoodsManager.getById(outsourcingGoodsId);
	}

	@Override
	public Pagination<OutsourcingGoodsWithBLOBs> queryOutsourcingGoods(String name,Boolean online, Sorting[] sorts, Page page) {
		return outsourcingGoodsManager.query(name,online, sorts, page);
	}

	@Override
	public void onlineOutsourcingGoods(OutsourcingGoodsWithBLOBs record, String updateUser) throws BizException {
		outsourcingGoodsManager.onlineGoods(record, updateUser);
	}

	@Override
	public void offlineOutsourcingGoods(OutsourcingGoodsWithBLOBs record, String updateUser) throws BizException {
		outsourcingGoodsManager.offlineGoods(record, updateUser);
	}
	
	@Autowired
	private OutsourcingPackagedGoodsManager outsourcingPackagedGoodsManager;
	
	@Override
	public boolean canOfflineOutsourcingGoods(Integer outsourcingGoodsId) throws BizException {
		return outsourcingGoodsManager.canOffline(outsourcingGoodsId);
	}
	
	@Autowired
	private OutsourcingPackagedManager outsourcingPackagedManager;

	@Override
	public OutsourcingPackaged addOutsourcingPackaged(OutsourcingPackaged record,
			List<Integer> outsourcingGoodsIds, String createUser) throws BizException {
		return outsourcingPackagedManager.add(record, outsourcingGoodsIds, createUser);
	}

	@Override
	public OutsourcingPackaged editOutsourcingPackaged(OutsourcingPackaged record,
			List<Integer> outsourcingGoodsIds, String updateUser) throws BizException {
		return outsourcingPackagedManager.edit(record, outsourcingGoodsIds, updateUser);
	}

	@Override
	public void deleteOutsourcingPackaged(OutsourcingPackaged record, String updateUser) throws BizException {
		outsourcingPackagedManager.delete(record, updateUser);
	}

	@Override
	public void onlineOutsourcingPackaged(OutsourcingPackaged record, String updateUser) throws BizException {
		outsourcingPackagedManager.online(record, updateUser);
	}

	@Override
	public void offlineOutsourcingPackaged(OutsourcingPackaged record, String updateUser) throws BizException {
		outsourcingPackagedManager.offline(record, updateUser);
	}

	@Override
	public OutsourcingPackaged getOutsourcingPackagedById(Integer outsourcingPackagedId) {
		return outsourcingPackagedManager.getById(outsourcingPackagedId);
	}

	@Override
	public Pagination<OutsourcingPackaged> queryOutsourcingPackaged(String name,Boolean online, Sorting[] sorts, Page page) {
		return outsourcingPackagedManager.query(name,online, sorts, page);
	}

	@Override
	public List<OutsourcingPackagedGoods> getOutsourcingPackagedGoodsByOutsourcingPackagedId(
			Integer outsourcingPackagedId) {
		return outsourcingPackagedGoodsManager.getByOutsourcingPackagedId(outsourcingPackagedId);
	}

	@Override
	public Pagination<OutsourcingGoodsWithBLOBs> queryOutsourcingGoodsForH5(Integer categoryId,String name, Sorting[] sorts,
			Page page) {
		return outsourcingGoodsManager.queryForH5(categoryId,name, sorts, page);
	}

	@Override
	public OutsourcingGoodsWithBLOBs getOutsourcingGoodsByIdForH5(Integer outsourcingGoodsId) {
		return outsourcingGoodsManager.getByIdForH5(outsourcingGoodsId);
	}

	@Override
	public Pagination<OutsourcingPackaged> queryOutsourcingPackagedForH5(Sorting[] sorts, Page page) {
		return outsourcingPackagedManager.queryForH5(sorts, page);
	}

	@Override
	public OutsourcingPackaged getOutsourcingPackagedByIdForH5(Integer outsourcingPackagedId) {
		return outsourcingPackagedManager.getByIdForH5(outsourcingPackagedId);
	}

	@Override
	public BigDecimal getTotalRetailPriceByPackagedId(Integer caseId) {
		return outsourcingPackagedManager.getTotalRetailPriceByPackagedId(caseId);
	}

	@Override
	public Pagination<QueryOutsourcingGoodsAndPackagedDTO> queryOutsourcingGoodsAndPackaged(String name, Page page) {
		return outsourcingGoodsManager.queryOutsourcingGoodsAndPackaged(name, page);
	}

	@Override
	public Pagination<OutsourcingPackaged> queryOutsourcingPackagedByGoodsId(Integer outsourcingGoodsId, Page page) {
		return outsourcingPackagedManager.queryOutsourcingPackagedByGoodsId(outsourcingGoodsId, page);
	}

	@Autowired
	private WholeHouseCaseManager wholeHouseCaseManager;
	
	@Autowired
	private UserWorksManager userWorksManager;
	
	
	@Override
	public List<QueryWholeHouseCaseAndUserWorksDTO> listByOutsourcingGoodsId(Integer outsourcingGoodsId, Integer size) {
		List<QueryWholeHouseCaseAndUserWorksDTO> list = new ArrayList<QueryWholeHouseCaseAndUserWorksDTO>();
		//取案例数据
		List<WholeHouseCase> wholeHouseCaseList = wholeHouseCaseManager.listWholeHouseCaseByOutsourcingGoodsId(outsourcingGoodsId, size);
		if(Check.notNullOrEmpty(wholeHouseCaseList)) {
			for (WholeHouseCase wholeHouseCase : wholeHouseCaseList) {
				QueryWholeHouseCaseAndUserWorksDTO dto = new QueryWholeHouseCaseAndUserWorksDTO();
				dto.setId(wholeHouseCase.getCaseId());
				dto.setName(wholeHouseCase.getName());
				dto.setImage(wholeHouseCase.getCoverImage());
				dto.setCreateTime(wholeHouseCase.getCreateTime());
				dto.setType(1);
				list.add(dto);
			}
		}
		//取作品数据
		List<UserWorks> userWorksList = userWorksManager.listByOutsourcinggoodsId(outsourcingGoodsId, size);
		if(Check.notNullOrEmpty(userWorksList)) {
			for (UserWorks userWorks : userWorksList) {
				QueryWholeHouseCaseAndUserWorksDTO dto = new QueryWholeHouseCaseAndUserWorksDTO();
				dto.setId(userWorks.getWorksId());
				dto.setName(userWorks.getTitle());
				dto.setImage(userWorks.getCoverImage());
				dto.setCreateTime(userWorks.getCreateTime());
				if(UserWorksType.OPUS.equal(userWorks.getWorksType())) {
					dto.setType(2);
				}else {
					dto.setType(3);
				}
				list.add(dto);
			}
		}
		//按照createTime倒序排列
		Collections.sort(list, new Comparator<QueryWholeHouseCaseAndUserWorksDTO>() {

			@Override
			public int compare(QueryWholeHouseCaseAndUserWorksDTO o1, QueryWholeHouseCaseAndUserWorksDTO o2) {
				return (int) (o2.getCreateTime().getTime()-o1.getCreateTime().getTime());
			}
			
		});
		for (int i = 0;i<list.size();i++) {
			//取前size条
			while(i>=size) {
				list.remove(list.get(i));
				i--;
			}
		}
		return list;
	}

	@Override
	public Pagination<WholeHouseCase> queryWholeHouseCase(Integer outsourcingGoodsId, Page page) {
		return wholeHouseCaseManager.queryByOutsourcingGoodsId(outsourcingGoodsId, page);
	}

	@Override
	public Pagination<UserWorks> queryUserWorks(Integer outsourcingGoodsId, Page page) {
		return userWorksManager.queryByOutsourcinggoodsId(outsourcingGoodsId, page);
	}

}
